• Maven,pom.xml中添加依赖
    1
    2
    3
    4
    5
    <dependency>
    <groupId>cn.shuibo</groupId>
    <artifactId>rsa-encrypt-body-spring-boot</artifactId>
    <version>1.0.0.RELEASE</version>
    </dependency>

  • 启动类Application中添加@EnableSecurity注解
    1
    2
    3
    4
    5
    6
    7
    @SpringBootApplication
    @EnableSecurity
    public class DemoApplication {
    public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
    }
    }

  • 在application.yml或者application.properties中添加RSA公钥及私钥
    1
    2
    3
    4
    5
    rsa:
    encrypt:
    debug: false # true表示开启调试,不加密。(方便开发时测试)
    publicKey: 123456
    privateKey: 123456

  • 对返回值进行加密
    1
    2
    3
    4
    5
    6
    7
    8
    @Encrypt
    @GetMapping("/encryption")
    public TestBean encryption(){
    TestBean testBean = new TestBean();
    testBean.setName("shuibo.cn");
    testBean.setAge(18);
    return testBean;
    }

  • 对传过来的加密参数解密
    1
    2
    3
    4
    5
    @Decrypt
    @PostMapping("/decryption")
    public String Decryption(@RequestBody TestBean testBean){
    return testBean.toString();
    }

参考